app.controller('SalesRouteListCtrl', function ($scope, ajax, $modal, toaster, $filter, $stateParams) {

    $scope.salesId = $stateParams.saleId;
    $scope.subject = $stateParams.subject;

    var inserter = new Req.POST({});
    var deleter = new Req.DELETE({});

    $scope.reload = $scope.page.loader(
        'sales/rt/salesRoute/{salesId}/{pageIndex}/{pageSize}/page'.format({ salesId: $scope.salesId }));
    $scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);

    //模糊查询
    $scope.queryMap = {};
    $scope.query = function () { $scope.reload(1, $scope.queryMap); };

    // 新增、更新
    $scope.write = function (size, current_data, submit_type) {
        $scope.current_data = angular.copy(current_data);
        $modal.open({
            templateUrl: 'template/rent/salesRoute/modal.html?v=afe164468f',
            controller: 'SalesRouteModalCtrl',
            size: size,
            resolve: {
                param: function () {
                    return {
                        "data": $scope.current_data,
                        "type": submit_type,
                        "salesId": $scope.salesId
                    };
                }
            }
        }).result.then(function (param) {
            inserter.fields = param.data;
            inserter.resource = 'sales/rt/salesRoute/insert';
            $scope.page.add(inserter, $scope.reload);
        });
    };

    //删除
    $scope.del = function (size, current_data, submit_type) {
        var modalInstance = $scope.delModal(size, current_data, submit_type);
        modalInstance.result.then(function (param) {
            if (param.type == 'delete') {
                deleter.id = param.data.id;
                deleter.resource = 'sales/rt/salesRoute/' + deleter.id + '/delete';
                $scope.page.del(deleter, $scope.reload);
            }
        });
    };

});
